/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.interfaces.studyword;

import goodenglish.application.StudyPlanService;
import goodenglish.application.impl.StudyPlanServiceImpl;
import goodenglish.domain.model.lexicon.StudyInfo;
import goodenglish.domain.model.lexicon.StudyPlan;
import goodenglish.interfaces.BeanFactory;
import goodenglish.interfaces.IndexPane;
import goodenglish.interfaces.Main;
import goodenglish.interfaces.MessageDialog;
import goodenglish.interfaces.javafxex.MyTextArea;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.web.HTMLEditor;

/**
 * 背诵单词界面
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class StudyWordPane extends BorderPane {

	private StudyPlan studyPlan;
	private Label wordLabel;
	private String emptyWordStr = "暂没有单词";
	private MyTextArea textArea;
	private int editHeight = 150;
	private int centenceHeight = 500;
	private int centenceWidth = 600;
	private HTMLEditor htmlEditor;
	private StudyInfo studyInfo;
	private Button seeAnswerButton;
	private Button rememberButton;
	private Button notRememberButton;
	private VBox rightVBox = new VBox();

	public StudyWordPane(StudyPlan studyPlan) {
		BorderPane.setAlignment(this, Pos.TOP_LEFT);
		this.studyPlan = studyPlan;
		this.setCenter(getCenterPane());
		this.setRight(getRightPane());

		displayNextWordSpell();

	}

	private VBox getCenterPane() {

		VBox vBox1 = new VBox();

		// 添加单词拼写行
		addWordSpellToLine2(vBox1);

		// 添加单词解释框行
		addExplainsToLine3(vBox1);

		// 添加例句显示框行
		addSentenceToLine4(vBox1);
		return vBox1;

	}

	private void addSentenceToLine4(VBox vBox1) {
		/**
		 * 添加下方的句子
		 */
		ScrollPane sentencePanel = new ScrollPane();
		sentencePanel.setMinWidth(centenceWidth);
		sentencePanel.setMaxWidth(centenceWidth);
		sentencePanel.setMinHeight(centenceHeight);
		sentencePanel.setStyle("-fx-border-color: black;-fx-border-width: 2;-fx-alignment:top-left;");
		htmlEditor = new HTMLEditor();
		htmlEditor.setPrefSize(centenceWidth, centenceHeight);
		htmlEditor.setMinWidth(centenceWidth);
		htmlEditor.setHtmlText("暂无句子");
		sentencePanel.setContent(htmlEditor);
		vBox1.getChildren().add(sentencePanel);
	}

	private void addWordSpellToLine2(VBox vBox1) {
		/**
		 * 添加中间
		 */
		wordLabel = new Label(emptyWordStr);
		wordLabel.setWrapText(true);
		wordLabel.setFont(new Font(100));
		wordLabel.setStyle("-fx-border-color: black;-fx-border-width: 2;");
		vBox1.getChildren().add(wordLabel);
	}

	private VBox getRightPane() {
		seeAnswerButton = new Button("看答案");
		seeAnswerButton.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent t) {

				displayWordInfo();
			}
		});
		rightVBox.getChildren().add(seeAnswerButton);
		seeAnswerButton.setFont(new Font(100));
		rememberButton = new Button("记得");
		rememberButton.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent t) {
				if (!wordLabel.getText().equals(emptyWordStr)) {
					lexiconService.addRightCount(studyInfo);
					displayNextWordSpell();
				}

			}
		});
		rememberButton.setFont(new Font(100));
		rememberButton.setTooltip(new Tooltip("快捷键：page_down"));
		notRememberButton = new Button("不记得");
		notRememberButton.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent t) {
				if (!wordLabel.getText().equals(emptyWordStr)) {

					lexiconService.addWrongCount(studyInfo);
					displayNextWordSpell();
				}

			}
		});
		notRememberButton.setFont(new Font(100));
		notRememberButton.setTooltip(new Tooltip("快捷键：page_up"));

		this.setOnKeyPressed(new EventHandler<KeyEvent>() {

			public void handle(KeyEvent t) {

				if (t.getCode().equals(KeyCode.PAGE_DOWN)) {

				}
				if (t.getCode().equals(KeyCode.PAGE_UP)) {
				}
			}
		});
		return rightVBox;
	}

	public void addExplainsToLine3(VBox vBox1) {
		HBox hBox = new HBox();
		hBox.setMaxWidth(centenceWidth);
		vBox1.getChildren().add(hBox);
		/**
		 * 添加解释编辑
		 */
		textArea = new MyTextArea("解释");
		textArea.setMaxHeight(editHeight);
		hBox.getChildren().add(textArea);
	}

	private void displayNextWordSpell() {
		studyInfo = studyPlan.getToStudyNewWord(studyInfo);

		if (studyInfo != null) {

			wordLabel.setText(studyInfo.getNewWord().getSpell());
			rightVBox.getChildren().clear();
			rightVBox.getChildren().add(seeAnswerButton);

			htmlEditor.setHtmlText("");
			textArea.setText("");
			copySpellToClipBoard();
			studyInfo.getNewWord().speek();
		} else {
			Main.instance.showModalMessage(new MessageDialog("恭喜你，学完了！！"));
			Main.root.setCenter(new IndexPane());
		}

	}

	public void displayWordInfo() {
		rightVBox.getChildren().clear();
		rightVBox.getChildren().add(rememberButton);
		rightVBox.getChildren().add(notRememberButton);

		htmlEditor.setHtmlText(studyInfo.getNewWord().getExampleSentStr());
		textArea.setText(null == studyInfo.getNewWord().getExplain() ? "" : studyInfo.getNewWord().getExplain());
		copySpellToClipBoard();

		studyInfo.getNewWord().speek();
	}

	private void copySpellToClipBoard() {
		if (studyInfo != null) {
			Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
			Transferable tText = new StringSelection(studyInfo.getNewWord().getSpell());
			clipBoard.setContents(tText, null);
		}

	}

	private StudyPlanService lexiconService = BeanFactory.getSingleInstance(StudyPlanServiceImpl.class);
}
